package core.form.election;

import java.util.ArrayList;

import core.form.IFormBackend;
import core.util.Converter;

public class ElectionFormBackend implements IFormBackend {
	private ArrayList<VotingCard> votingCardList;

	public ElectionFormBackend() {
		votingCardList = new ArrayList<>();
	}

	public ArrayList<VotingCard> getVotingCardList() {
		return votingCardList;
	}

	public String getVoteInXML() {
		return Converter.objectToXML(votingCardList);
	}

	public void addVotingCard(VotingCard votingCard) {
		votingCardList.add(votingCard);
	}

	public void removeVotingCard(VotingCard votingCard) {
		votingCardList.remove(votingCard);
	}

	@Override
	public void clean() {
		for (VotingCard votingCard : votingCardList) {
			for (VotingCardOption votingCardOption : votingCard.getVotingCardOptionList()) {
				if (votingCardOption.isChecked()) {
					votingCardOption.check();
				}
			}
		}
	}

	@Override
	public String toString() {
		String output = "";

		output += "backend:\n";

		int votingCardIdx = 0;
		for (VotingCard votingCard : votingCardList) {
			output += "\t" + votingCardIdx++ + ": " + votingCard.getName() + "\n";
			int votingCardOptionIdx = 0;
			for (VotingCardOption votingCardOption : votingCard.getVotingCardOptionList()) {
				output += "\t\t" + votingCardOptionIdx++ + ": " + votingCardOption.getName() + "\n";
			}
		}

		return output;
	}
}
